package newspaperSystem;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/**
 *
 * @author Qiqa Leo Kotyi
 */
public class NewspaperSystem extends JFrame implements ActionListener {

    String[] tru = {"Qiqa", "Kotyi"};
    Delete delete = new Delete();
    // Login Form
    private JLabel formLabel = new JLabel("Welcome to Newspaper delivery");
    private JLabel loginLabel = new JLabel("User name:");
    private JLabel passLabel = new JLabel("Password:");
    private JTextField loginTxt = new JTextField(10);
    private JTextField passTxt = new JTextField(10);
    private JButton btnLogin = new JButton("Login");
    private JButton btnClose = new JButton("Close");
    private JPanel northPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JLabel picLabel;
    private JLabel timeLabel;
    private Date date = new Date();
    /// end of Login Form
    // Home Form
    private JFrame homframe = new JFrame("Home");
    private JLabel hmeLabel = new JLabel("Newspaper Delivery");
    private JLabel welcomeLabel = new JLabel("Welcome to Newspaper delivery Service");
    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JPanel cnterPanel = new JPanel();
    private JPanel leftPanel = new JPanel();
    private JPanel rightPanel = new JPanel();
    private JButton loadBtn = new JButton("Load tables");
    private JRadioButton carrierRadio = new JRadioButton("Carriers");
    private JRadioButton custRadio = new JRadioButton("Customers");
    private JRadioButton prodRadio = new JRadioButton("Products");
    private JRadioButton subscrRadio = new JRadioButton("Subscription");
    private JRadioButton routesRadio = new JRadioButton("Routes");
    private JRadioButton billRadio = new JRadioButton("Bill");
    private JRadioButton suppRadio = new JRadioButton("Suppliers");
    private JRadioButton dummyRadio = new JRadioButton("");
    private ButtonGroup btnG = new ButtonGroup();
    private JLabel pic2Label;
    private JButton insertBtn = new JButton("insert");
    private JButton updateBtn = new JButton("update");
    private JButton deleteBtn = new JButton("delete");
    private JButton searchBtn = new JButton("search");
    private JButton paymentBtn = new JButton("Make payment");
    private JTextArea homeArea = new JTextArea(30, 40);
    private JScrollPane homeScroll = new JScrollPane(homeArea);
    // private JButton bckBtn = new JButton("Back");
    private JButton closeBtn = new JButton("Log Out");
    // end of Home form
    //Insert Form
    
    Addform addForm = new Addform(); 
    private JFrame insertFrame = new JFrame("Insert");
    private JLabel insertLabel = new JLabel("Add New Data");
    private JButton carrierBtn = new JButton("Carriers");
    
    //insert customer
    InsertCustomer insertCustomer = new InsertCustomer();
    //Insert Product 
    InsertProduct insertProduct = new InsertProduct();
    
    //Carrier insert frame
    private JLabel insertCarLabel = new JLabel("Add New Carrier");
    private JFrame carFrame = new JFrame("Add Carrier");
    private JLabel nameLbl = new JLabel("Name:");
    private JTextField nameTxt = new JTextField(15);
    private JLabel snameLbl = new JLabel("Surname:");
    private JTextField snameTxt = new JTextField(15);
    private JLabel phneLbl = new JLabel("Phone number:");
    private JTextField phneTxt = new JTextField(15);
    private JLabel addrLbl = new JLabel("Address:");
    private JTextField addrTxt = new JTextField(20);
    private JLabel carIDLbl = new JLabel("Carrier_Id:");
    private JTextField carrierId = new JTextField(20);
    private JLabel surbabLbl = new JLabel("Surbab:");
    private JTextField surbabTxt = new JTextField(20);
    private JButton carInsBtn = new JButton("Insert");
    private JButton carBkBtn = new JButton("Back");
    private JPanel carrierWpanel = new JPanel();
    private JPanel carrierSpanel = new JPanel();
    private JPanel carrierCpanel = new JPanel();
    private JPanel carrierNpanel = new JPanel();
    private JTextArea insertCArea = new JTextArea(20, 25);
    //end of carrier frame add
    private JButton custBtn = new JButton("Customer");
    private JButton productBtn = new JButton("Products");
    private JButton subscriptionBtn = new JButton("Subscription");
    private JButton routeBtn = new JButton("Routes");
    private JButton billBtn = new JButton("Bill");
    private JButton suppBtn = new JButton("Suppliers");
    private JButton insBtn = new JButton("Insert");
    private JButton bkBtn = new JButton("Back");
    // private JTextArea insertArea = new JTextArea(20, 25);
    private JPanel nPanel = new JPanel();
    private JPanel westPanel = new JPanel();
    private JPanel sPanel = new JPanel();
    private JPanel cPanel = new JPanel();
    //End of insert form
    private JRadioButton crRadio = new JRadioButton("Carriers");
    private JRadioButton cuRadio = new JRadioButton("Customers");
    private JRadioButton prRadio = new JRadioButton("Products");
    private JRadioButton subRadio = new JRadioButton("Subscription");
    private JRadioButton routRadio = new JRadioButton("Routes");
    private JRadioButton blRadio = new JRadioButton("Bill");
    private JRadioButton spRadio = new JRadioButton("Suppliers");
    //update form
    private updateForm loadUpdate = new updateForm();
    //end of update frame
    // Payment Form
    private JFrame paymentFrame = new JFrame("Make Payment");
    private JLabel paymentLabel = new JLabel("Make Payment");
    private JRadioButton unpaidRadio = new JRadioButton("Check unpaid");
    private JRadioButton paidRadio = new JRadioButton("Check paid");
    private JRadioButton dispRadio = new JRadioButton("Display All");
    private ButtonGroup payBtnG = new ButtonGroup();
    private JLabel amntLbl = new JLabel("Amount");
    private JTextField amntTxt = new JTextField(10);
    private JLabel totalLbl = new JLabel("Total");
    private JTextField totalTxt = new JTextField(10);
    private JButton enterBtn = new JButton("Enter");
    private JTextArea payArea = new JTextArea(25, 35);
    private JButton paybkBtn = new JButton("Back To top Menu");
    private JScrollPane listScroll = new JScrollPane(payArea);
    private JPanel paySouthPnl = new JPanel();
    private JPanel radioPanel = new JPanel();
    private JPanel payPanel = new JPanel();
    private JPanel listPanel = new JPanel();
    private JPanel payLblPanel = new JPanel();
    private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");

    //end of payment Frame
    public NewspaperSystem() {

        super("Newspaper system");
        setLayout(new BorderLayout());
        //Login form
        northPanel.setLayout(new FlowLayout());
        southPanel.setLayout(new FlowLayout());
        centerPanel.setLayout(new FlowLayout());

        timeLabel = new JLabel((dateFormat.format(date)));
        timeLabel.setFont(new Font("Serif", Font.ITALIC, 16));
        timeLabel.setForeground(Color.RED);

        picLabel = new JLabel(new ImageIcon("newspapers.jpg"));
        northPanel.add(formLabel);

        centerPanel.add(loginLabel);
        centerPanel.add(loginTxt);
        centerPanel.add(passLabel);
        centerPanel.add(passTxt);
        centerPanel.add(picLabel);

        southPanel.add(timeLabel);
        southPanel.add(btnLogin);
        southPanel.add(btnClose);

        add(northPanel, BorderLayout.NORTH);
        add(southPanel, BorderLayout.SOUTH);
        add(centerPanel, BorderLayout.CENTER);

        //End of login form

        //home form
        topPanel.setLayout(new FlowLayout());
        bottomPanel.setLayout(new FlowLayout());
        leftPanel.setLayout(new GridLayout(12, 2, 5, 5));
        rightPanel.setLayout(new GridLayout(12, 1, 5, 5));

        hmeLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
        welcomeLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
        // topPanel.add(hmeLabel);
        topPanel.add(welcomeLabel);

        btnG.add(carrierRadio);
        btnG.add(custRadio);
        btnG.add(prodRadio);
        btnG.add(subscrRadio);
        btnG.add(routesRadio);
        btnG.add(suppRadio);
        btnG.add(billRadio);
        btnG.add(dummyRadio);


        leftPanel.add(carrierRadio);
        rightPanel.add(custRadio);
        leftPanel.add(prodRadio);
        rightPanel.add(subscrRadio);
        leftPanel.add(routesRadio);
        rightPanel.add(suppRadio);
        rightPanel.add(billRadio);
        leftPanel.add(dummyRadio);
        dummyRadio.setVisible(false);
        dummyRadio.setVisible(false);

        leftPanel.add(searchBtn);
        rightPanel.add(insertBtn);
        leftPanel.add(updateBtn);
        rightPanel.add(deleteBtn);
        leftPanel.add(paymentBtn);


        // bottomPanel.add(bckBtn);
        bottomPanel.add(closeBtn);
        homeArea.setEditable(false);
        //homeArea.setBackground(Color.gray);


        homeArea.setBorder(BorderFactory.createLineBorder(Color.RED));

        cnterPanel.add(homeArea);
        cnterPanel.add(timeLabel);
        homeScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        homeScroll.setPreferredSize(new Dimension(250, 250));


        homframe.add(rightPanel, BorderLayout.EAST);
        homframe.add(topPanel, BorderLayout.NORTH);
        homframe.add(leftPanel, BorderLayout.WEST);
        homframe.add(bottomPanel, BorderLayout.SOUTH);
        homframe.add(cnterPanel, BorderLayout.CENTER);

        //End of home form

        // Insert form
        insertFrame.setLayout(new BorderLayout());
        nPanel.setLayout(new FlowLayout());

        westPanel.setLayout(new GridLayout(15, 1));
        sPanel.setLayout(new FlowLayout());
        // cPanel.setLayout(new GridLayout(7,1,6,6));
        insertLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
        nPanel.add(insertLabel);


        cPanel.add(carrierBtn);
        cPanel.add(custBtn);
        cPanel.add(productBtn);
        cPanel.add(subscriptionBtn);
        cPanel.add(routeBtn);
        cPanel.add(billBtn);
        cPanel.add(suppBtn);

        pic2Label = new JLabel(new ImageIcon("image.jpg"));
        cPanel.add(pic2Label);

        sPanel.add(insBtn);
        sPanel.add(bkBtn);

        insertFrame.add(nPanel, BorderLayout.NORTH);
        insertFrame.add(westPanel, BorderLayout.WEST);
        insertFrame.add(cPanel, BorderLayout.CENTER);
        insertFrame.add(sPanel, BorderLayout.SOUTH);
        // End of insert Frame 
        // Insert carrier frame
        carrierWpanel.setLayout(new GridLayout(15, 1));
        carrierSpanel.setLayout(new FlowLayout());
        carrierNpanel.setLayout(new FlowLayout());

        carrierCpanel.add(insertCArea);
        carrierSpanel.add(carInsBtn);
        carrierSpanel.add(carBkBtn);
        carrierNpanel.add(insertCarLabel);

        carrierWpanel.add(carIDLbl);
        carrierWpanel.add(carrierId);
        carrierWpanel.add(nameLbl);
        carrierWpanel.add(nameTxt);
        carrierWpanel.add(snameLbl);
        carrierWpanel.add(snameTxt);

        carrierWpanel.add(phneLbl);
        carrierWpanel.add(phneTxt);
        carrierWpanel.add(addrLbl);
        carrierWpanel.add(addrTxt);
        carrierWpanel.add(surbabLbl);
        carrierWpanel.add(surbabTxt);

        carFrame.add(carrierWpanel, BorderLayout.WEST);
        carFrame.add(carrierCpanel, BorderLayout.CENTER);
        carFrame.add(carrierSpanel, BorderLayout.SOUTH);
        carFrame.add(carrierNpanel, BorderLayout.NORTH);
        //end of carrier add


        //Payment form
        radioPanel.setLayout(new GridLayout(10, 1));
        payPanel.setLayout(new GridLayout(12, 2, 5, 5));
        payLblPanel.setLayout(new FlowLayout());
        paySouthPnl.setLayout(new FlowLayout());

        // listPanel.setLayout(new FlowLayout());
        payArea.setBorder(BorderFactory.createLineBorder(Color.RED));
        payBtnG.add(unpaidRadio);
        payBtnG.add(paidRadio);
        payBtnG.add(dispRadio);

        paymentLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
        radioPanel.add(unpaidRadio);
        radioPanel.add(paidRadio);
        radioPanel.add(dispRadio);

        payPanel.add(amntLbl);
        payPanel.add(amntTxt);
        payPanel.add(totalLbl);
        payPanel.add(totalTxt);
        payPanel.add(enterBtn);
        payLblPanel.add(paymentLabel);
        paySouthPnl.add(paybkBtn);

        listScroll.setPreferredSize(new Dimension(200, 200));

        // Dimension dim = payList.getPreferredSize();
        // payList.setBounds(300, 300, (int) dim.getWidth(), (int) dim.getHeight());


        // listPanel.add(payList);
        listPanel.add(payArea);
        paymentFrame.add(paySouthPnl, BorderLayout.SOUTH);
        paymentFrame.add(radioPanel, BorderLayout.WEST);
        paymentFrame.add(payPanel, BorderLayout.EAST);
        paymentFrame.add(listPanel, BorderLayout.CENTER);
        paymentFrame.add(payLblPanel, BorderLayout.NORTH);
        //End payment form
        // Listeners

        btnLogin.addActionListener(this);
        btnClose.addActionListener(this);

        insertBtn.addActionListener(this);
        deleteBtn.addActionListener(this);

        searchBtn.addActionListener(this);
        updateBtn.addActionListener(this);
        loadBtn.addActionListener(this);
        closeBtn.addActionListener(this);

        carrierRadio.addActionListener(this);
        custRadio.addActionListener(this);
        prodRadio.addActionListener(this);
        subscrRadio.addActionListener(this);
        routesRadio.addActionListener(this);
        billRadio.addActionListener(this);
        suppRadio.addActionListener(this);

        carrierBtn.addActionListener(this);
        carInsBtn.addActionListener(this);

        paymentBtn.addActionListener(this);
        unpaidRadio.addActionListener(this);
        paidRadio.addActionListener(this);
        dispRadio.addActionListener(this);
        paybkBtn.addActionListener(this);
        enterBtn.addActionListener(this);

        cuRadio.addActionListener(this);
        crRadio.addActionListener(this);
        prRadio.addActionListener(this);
        subRadio.addActionListener(this);
        routRadio.addActionListener(this);
        blRadio.addActionListener(this);
        spRadio.addActionListener(this);
        bkBtn.addActionListener(this);
        custBtn.addActionListener(this);
        productBtn.addActionListener(this);


    }

    @Override
    public void actionPerformed(ActionEvent e) {



        // List list = new ArrayList();
        //    DefaultListModel model = new DefaultListModel();

        Locale loc = Locale.getDefault();
        ResourceBundle bundle = ResourceBundle.getBundle("database", loc);
        String url = bundle.getString("URL");
        String user = bundle.getString("username");
        String pw = bundle.getString("password");
        String driver = bundle.getString("Driver");
        Connection conn = null;
        Statement stmt = null;
        String sqlStmt = "Select username ,password from Hr ";
        //PreparedStatement pstmnt;

        ResultSet resultSet = null;

        try {

            Class.forName(driver);

        } catch (ClassNotFoundException classNotFound) {

            System.out.println("OOps class not found:" + classNotFound);
        }


        try {

            conn = DriverManager.getConnection(url, user, pw);

            System.out.println("Connection successful");
            stmt = conn.createStatement();

            resultSet = stmt.executeQuery(sqlStmt);

            while (resultSet.next()) {

                String username = loginTxt.getText();
                String password = passTxt.getText();


                try {

                    if (username.equals(resultSet.getString("username"))) {

                        if (password.equals(resultSet.getString("password"))) {

                            homframe.setSize(800, 700);
                            homframe.setVisible(true);
                            break;
                        } else {
                            JOptionPane.showMessageDialog(null, "Wrong password");
                            break;
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Wrong User name");
                        break;
                    }
                } catch (SQLException excep) {
                    System.out.println(excep);
                }


            }
            if (e.getSource() == carrierRadio) {


                String query = "Select* from Carriers";


                resultSet = stmt.executeQuery(query);

                if (resultSet != null) {
                    homeArea.setText("Carrier Id\tName\tSurname\tPhone Number\tAddress\tSurbab\n");
                }
                homeArea.append("=====================================================================\n");

                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t" + resultSet.getString(4) + "\t" + resultSet.getString(5) + "\t" + resultSet.getString(6) + "\n");
                }

            }
            if (e.getSource() == custRadio) {

                String query2 = "Select* from Customer";
                resultSet = stmt.executeQuery(query2);

                if (resultSet != null) {
                    homeArea.setText("Customer Id\tName\tSurname\tPhone Number\tAddress\tSurbab\n");
                }
                homeArea.append("==============================================================================\n");

                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t" + resultSet.getString(4) + "\t" + resultSet.getString(5) + "\t" + resultSet.getString(6) + "\n");
                }
            }
            if (e.getSource() == prodRadio) {

                String query3 = "Select* from Product";
                resultSet = stmt.executeQuery(query3);

                if (resultSet != null) {
                    homeArea.setText("Product Id\tSubscription Id\tProduct descr\tProduct Price\n");
                }
                homeArea.append("=============================================================\n");
                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t" + resultSet.getString(4) + "\n");
                }
            }
            if (e.getSource() == subscrRadio) {

                String query4 = "Select* from Subscription";
                resultSet = stmt.executeQuery(query4);

                if (resultSet != null) {
                    homeArea.setText("Subscription Id\tCustomer Id\tType\tDescription\n");
                }
                homeArea.append("=========================================================\n");
                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t" + resultSet.getString(4) + "\n");
                }
            }
            if (e.getSource() == routesRadio) {

                String query5 = "Select* from Routes";
                resultSet = stmt.executeQuery(query5);

                if (resultSet != null) {
                    homeArea.setText("Route Number\tRoute Name\tRoute description\tRoute Address\n");
                }
                homeArea.append("===============================================================\n");
                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t\t" + resultSet.getString(4) + "\n");
                }

            }
            if (e.getSource() == billRadio) {

                String query6 = "Select* from Bill";
                resultSet = stmt.executeQuery(query6);

                if (resultSet != null) {
                    homeArea.setText("Customer Id\tCustomer Name\tSubscription Type\tAmount\n");
                }
                homeArea.append("=====================================================================\n");
                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3) + "\t\t" + resultSet.getString(4) + "\n");
                }

            }
            if (e.getSource() == suppRadio) {

                String query7 = "Select* from Suppliers";
                resultSet = stmt.executeQuery(query7);

                if (resultSet != null) {
                    homeArea.setText("Supplier Name \t\tDate\n");
                }
                homeArea.append("========================================\n");
                while (resultSet.next()) {

                    homeArea.append(resultSet.getString(1) + "\t\t" + resultSet.getString(2) + "\n");
                }

            }
            if (e.getSource() == insertBtn) {
                
                addForm.setVisible(true);
            } else if (e.getSource() == carrierBtn) {

                carFrame.setSize(700, 600);
                carFrame.setVisible(true);
            }

             else if (e.getSource() == deleteBtn) {


                delete.setVisible(true);
            } else if (e.getSource() == updateBtn) {

                //updateFrame.setSize(1000,600);
                //updateFrame.setVisible(true);
                loadUpdate.setVisible(true);
            } else if (e.getSource() == paymentBtn) {

                paymentFrame.setSize(700, 600);
                paymentFrame.setVisible(true);
                homframe.setVisible(false);

            }

            if (e.getSource() == unpaidRadio) {

                homframe.setVisible(false);

                String unpaidQuery = "Select* from Bill";
                resultSet = stmt.executeQuery(unpaidQuery);

                while (resultSet.next()) {

                    if (resultSet.getDouble(4) <= 0) {


                        payArea.setText("Customer Id\tCustomer Name\tSubscription Type\tAmount\n");
                        payArea.append("=====================================================================\n");


                        payArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3) + "\t\t" + resultSet.getString(4) + "\n");
                    }
                }
            }
            if (e.getSource() == paidRadio) {
                homframe.setVisible(false);
                String paidQuery = "Select* from Bill";
                resultSet = stmt.executeQuery(paidQuery);

                while (resultSet.next()) {

                    if (resultSet.getDouble(4) >= 1) {

                        payArea.setText("Customer Id\tCustomer Name\tSubscription Type\tAmount\n");
                        payArea.append("=====================================================================\n");

                        payArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3) + "\t\t" + resultSet.getString(4) + "\n");
                    }
                }
            }
            if (e.getSource() == dispRadio) {

                homframe.setVisible(false);
                String dispQuery = "Select* from Bill";
                resultSet = stmt.executeQuery(dispQuery);

                if (resultSet != null) {
                    payArea.setText("Customer Id\tCustomer Name\tSubscription Type\tAmount\n");
                }
                payArea.append("=====================================================================\n");
                while (resultSet.next()) {

                    payArea.append(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3) + "\t\t" + resultSet.getString(4) + "\n");
                }

            }
            if (e.getSource() == enterBtn) {

                String amnt = amntTxt.getText();
                double amount = Double.parseDouble(amnt);

                PreparedStatement psmt;
                String billQuery = "Select * from Bill";

                resultSet = stmt.executeQuery(billQuery);

                while (resultSet.next()) {
                    if (resultSet.getDouble(4) <= 0) {

                        psmt = conn.prepareStatement("insert into BILL(Amount)values(?)");
                        psmt.setDouble(4, amount);
                        psmt.executeUpdate();
                    }
                }

            } else if (e.getSource() == btnClose) {
                System.exit(0);
            }
            if (e.getSource() == paybkBtn) {

                paymentFrame.dispose();
                homframe.setVisible(true);

            } else if (e.getSource() == closeBtn) {

                homframe.dispose();
            }
            if (e.getSource() == bkBtn) {
                paymentFrame.dispose();
                carFrame.dispose();
                insertFrame.dispose();
            }
            if(e.getSource()==custBtn){
                insertCustomer.setVisible(true); 
            }
            if(e.getSource()==productBtn){
            
                insertProduct.setVisible(true);
            }
        } catch (SQLException excep) {
            System.out.println(excep);
        } finally {

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (Exception exception) {
                System.out.println("Something went wrong along the way");
            }
        }
    }
}